package com.css.fxfzxczh.modules.fieldWorkManage.noticeManage.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzxczh.base.response.RestResponse;
import com.css.fxfzxczh.modules.fieldWorkManage.InvestigationGroupManage.entity.InvestigationTeam;
import com.css.fxfzxczh.modules.fieldWorkManage.noticeManage.entity.Notice;
import com.css.fxfzxczh.modules.fieldWorkManage.noticeManage.param.NoticePageParam;
import com.css.fxfzxczh.modules.fieldWorkManage.noticeManage.param.NoticeSaveParam;
import com.css.fxfzxczh.modules.fieldWorkManage.noticeManage.service.NoticeService;
import com.css.fxfzxczh.modules.fieldWorkManage.noticeManage.vo.NoticeSaveParamVO;
import com.css.fxfzxczh.util.PlatformPageUtils;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/02/02
 * @Description: 通知管理
 */
@RestController
@RequestMapping("/notice")
@Slf4j
@Api(tags = "通知管理")
public class NoticeController {

    @Resource
    private NoticeService noticeService;

    /**
     * 分页查询
     *
     * @param queParams
     * @return
     */
    @GetMapping("/getPage")
    public RestResponse getPage(NoticePageParam queParams, HttpServletRequest request) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> page = noticeService.getPage(queParams, curPage, pageSize);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(page);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 新增单条数据
     *
     * @param param
     * @return
     */
    @PostMapping("/save")
    @Operation(summary = "新增数据")
    public RestResponse save(@RequestBody NoticeSaveParam param) {
        try {
            noticeService.save(param);
            return RestResponse.succeed("新增成功");
        } catch (Exception e) {
            log.error("新增数据失败!", e);
            return RestResponse.fail("新增数据失败!");
        }
    }

    /**
     * 删除单条数据
     *
     * @param Id
     * @return
     */
    @PostMapping("/deleteById/{id}")
    @Operation(summary = "删除单条数据")
    public RestResponse deleteById(@PathVariable("id") String Id) {
        if (StringUtils.isBlank(Id)) {
            return RestResponse.fail("Id不能为空!");
        }
        try {
            noticeService.deleteById(Id);
            return RestResponse.succeed("删除成功");
        } catch (Exception e) {
            log.error("删除数据失败!", e);
            return RestResponse.fail("删除数据失败!");
        }
    }

    /**
     * 修改单条数据
     *
     * @param param
     * @return
     */
    @PostMapping("/updateById")
    @Operation(summary = "修改单条数据")
    public RestResponse updateById(@RequestBody NoticeSaveParam param) {
        try {
            noticeService.updateById(param);
            return RestResponse.succeed("修改成功");
        } catch (Exception e) {
            log.error("修改数据失败!", e);
            return RestResponse.fail("修改数据失败!");
        }
    }

    @GetMapping("/findById")
    @Operation(summary = "根据id查询")
    public RestResponse findById(String id) {
        try {
            NoticeSaveParamVO data = noticeService.findById(id);
            return RestResponse.succeed(data);
        } catch (Exception e) {
            log.error("失败!", e);
            return RestResponse.fail("失败!");
        }
    }

    @GetMapping("/getCode")
    @Operation(summary = "获取通知code")
    public RestResponse getCode(String xsemId) {
        try {
            Notice notice = noticeService.getCode(xsemId);
            return RestResponse.succeed(notice);
        } catch (Exception e) {
            log.error("失败!", e);
            return RestResponse.fail("失败!");
        }
    }

    @GetMapping("/getItemAll")
    @Operation(summary = "获取所有的调查组")
    public RestResponse getItemAll(String xsemId) {
        try {
            List<InvestigationTeam> list = noticeService.getItemAll(xsemId);
            return RestResponse.succeed(list);
        } catch (Exception e) {
            log.error("失败!", e);
            return RestResponse.fail("失败!");
        }
    }
}
